package com.example.project.vo;

import com.example.project.enums.HttpStatusCode;

public class Result<T> {

	/**
	 * 响应状态码
	 */
	private int code;

	/**
	 * 响应提示
	 */
	private String message;

	/**
	 * 响应数据
	 */
	private T data;

	// 成功响应（带数据）
	public static <T> Result<T> success(T data) {
		Result<T> result = new Result<>();
		result.code = HttpStatusCode.SUCCESS.getCode();
		result.message = HttpStatusCode.SUCCESS.getMessage();
		result.data = data;
		return result;
	}

	// 成功响应（无数据）
	public static <T> Result<T> success() {
		return success(null);
	}

	public static <T> Result<T> fail(HttpStatusCode httpStatus) {
		Result<T> response = new Result<>();
		response.setCode(httpStatus.getCode());
		response.setMessage(httpStatus.getMessage());
		return response;
	}

	public static <T> Result<T> failed(HttpStatusCode resultCode, String message) {
		return new Result<T>(resultCode.getCode(), message, null);
	}

	public static <T> Result<T> failed(HttpStatusCode resultCode, T data) {
		return new Result<T>(resultCode.getCode(), resultCode.getMessage(), data);
	}

	// 错误响应（仅错误码）
	public static <T> Result<T> error(HttpStatusCode errorCode) {
		return error(errorCode, errorCode.getMessage());
	}

	// 错误响应（完整参数：错误码 + 消息）⭐ 满足需求
	public static <T> Result<T> error(HttpStatusCode errorCode, String message) {
		Result<T> result = new Result<>();
		result.code = errorCode.getCode();
		result.message = message;
		return result;
	}

	public static <T> Result<T> error(int code, String message) {
		// TODO Auto-generated method stub
		Result<T> result = new Result<>();
		result.code = code;
		result.message = message;
		return result;
	}

	public Result() {
	}

	public Result(Integer code, String message, T data) {
		this.code = code;
		this.message = message;
		this.data = data;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

}